package cn.dw.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import cn.dw.utils.HttpUtils;
import net.sf.json.JSONObject;

public class weatherByGPS {

	

	private JSONObject result;

	public static void main(String[] args) {
	  //  JsonObject gson = weatherInfo("40.242266", "116.2278");
	    
	  //  System.out.println(gson);
	}
	/*
	@RequestMapping(value = "/getJsonData", method = RequestMethod.GET)
	public @ResponseBody String getJsonData(HttpServletRequest request){
	    
		String latitude = request.getParameter("lat");

		String longitude = request.getParameter("lng");
		
		//String data = weatherInfo(latitude, longitude).toString();
		
	    return null;
	}*/
	
	public String weatherInfo(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		HttpServletResponse response1 = ServletActionContext.getResponse();
		
		response1.setContentType("application/json;charset=utf-8");
        response1.setHeader("Access-Control-Allow-Origin", "*");
		
		String latitude = request.getParameter("lat");

		String longitude = request.getParameter("lng");
		
		JsonObject nowWeather_info = null;
		System.out.println(latitude);
		String host = "http://saweather.market.alicloudapi.com";
	    String path = "/gps-to-weather";
	    String method = "GET";
	    String appcode = "85038184f8b34a27bd30a2ed990a632f";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("from", "5");
	    querys.put("lat", latitude);
	    querys.put("lng", longitude);
	    querys.put("need3HourForcast", "0");
	    querys.put("needAlarm", "0");
	    querys.put("needHourData", "0");
	    querys.put("needIndex", "0");
	    querys.put("needMoreDay", "0");

	    Map<String, Object> map = new HashMap<String,Object>();
	    
	    try {
	    	
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	String result = EntityUtils.toString(response.getEntity());
	    	
	    	JsonParser jp = new JsonParser();

			JsonElement je = jp.parse(result);

			JsonObject jo = je.getAsJsonObject();

			je = jo.get("showapi_res_body");
			
			jo = je.getAsJsonObject();
			
			JsonElement nowWeather = jo.get("now");
			
			nowWeather_info = nowWeather.getAsJsonObject();
			
			String temperature = nowWeather_info.get("temperature").getAsString();
			
			map.put("temperature", temperature+"°");

	    //	System.out.println("112323====="+je);
	    	//获取response的body
	    	//System.out.println(EntityUtils.toString(response.getEntity()));
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    
	    System.out.println(nowWeather_info.toString());
		
	    JSONObject json = JSONObject.fromObject(nowWeather_info.toString());
	    
	    this.result = json;
	     
	   /* try {
			response1.getWriter().write(json.toString());;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	    
		return "weatherInfo";
	}
	
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}
}
